import { reqC1, reqC2 } from '@/apis/product/attr'
import type { CategoryObj } from '@/types/attr'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useCategoryStore = defineStore('category', () => {
  //存储一级分类数据
  const c1Arr = ref<CategoryObj[]>([])
  //存储二级类数据
  const c2Arr = ref<CategoryObj[]>([])
  //存储三级类数据
  const c3Arr = ref<CategoryObj[]>([])
  // 一级分类的id
  const c1Id = ref<number | string>('')
  //二级分类的id
  const c2Id = ref<number | string>('')
  //三级分类的id
  const c3Id = ref<number | string>('')
  //获取一级分类数据
  const getC1 = async () => {
    const res = await reqC1()
    if (res.code === 200) {
      c1Arr.value = res.data
    }
  }
  //获取二级分类数据
  const getC2 = async () => {
    const res = await reqC2(c1Id.value)
    if (res.code === 200) {
      c2Arr.value = res.data
    }
  }
  //获取三级分类数据
  const getC3 = async () => {
    const res = await reqC2(c2Id.value)
    if (res.code === 200) {
      c3Arr.value = res.data
    }
  }
  //导出数据
  return {
    c1Arr,
    c1Id,
    c2Arr,
    c3Arr,
    c2Id,
    c3Id,
    getC1,
    getC2,
    getC3,
  }
})
